import { ref, computed } from "vue";
import { defineStore } from "pinia";

export const useSearchedAccidentsStore = defineStore(
  "searchedAccidents",
  () => {
    const searchedAccidents = ref([]);//存储搜索结果
    const searchedTotal = computed(() => searchedAccidents.value.length);//搜索结果总数
    const panData = ref([]);//存储生成饼图及折线图所需的数据
    const hotData = ref({
      type: "FeatureCollection",
      features: [],
    });//存储热力图所需的数据

    /* 获取数据后填充数组 */
    const addAccidentSearch = (newSearch) => {
      //console.log(newSearch)
      searchedAccidents.value.push(newSearch);
      /* 将属性添加到饼图及折线图数据 */
      panData.value.push(newSearch.attr);
      /* 将坐标转为geometry，并添加到热力图数据 */
      hotData.value.features.push({
        type: "Feature",
        geometry: {
          type: "Point",
          coordinates: [newSearch.coord],
        },
        properties: {
            weight: Number(newSearch.attr.事件等级)/4,
        }
      });
    };

    /* 重置数据 */
    const resetData = () => {
      searchedAccidents.value = [];
      panData.value = [];
      hotData.value.features = [];
    };
    return {
      searchedAccidents,
      searchedTotal,
      panData,
      hotData,
      addAccidentSearch,
      resetData,
    };
  }
);
